package com.crm.miaohe.utils;

import org.springframework.util.Base64Utils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class MD5Utils {

    public static String hashMAC(String data, String secret) {
        try {
            Mac kdmac = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            kdmac.init(secret_key);
            byte[] rawHmac = kdmac.doFinal(data.getBytes());
            return Base64Utils.encodeToString(bytesToHex(rawHmac).getBytes());
        } catch (Exception var5) {
            var5.printStackTrace();
            return null;
        }
    }

    private static String bytesToHex(byte[] hashInBytes) {
        StringBuilder sb = new StringBuilder();

        for (byte hashInByte : hashInBytes) {
            String hex = Integer.toHexString(hashInByte & 255);
            if (hex.length() < 2) {
                hex = "0" + hex;
            }

            sb.append(hex);
        }

        return sb.toString();
    }
}
